﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.Text;

namespace Server.Common
{
    public static class Common
    {
        public static dynamic DecodeParameter(dynamic param)
        {
            IDictionary<string, object> dic = null;
            if (param is System.Dynamic.ExpandoObject)
            {
                dic = (IDictionary<string, object>)param;
            }
            else
            {
                foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(param))
                {
                    dic.Add(propertyDescriptor.Name, propertyDescriptor.GetValue(param));
                }
            }

            dynamic result = new ExpandoObject();

            //Dictionary<string, object> result = new Dictionary<string, object>();
            foreach (var item in dic.Keys)
            {
                object value = null;

                if (dic[item].GetType().Name == "String")
                {
                    value = Transcoding(dic[item].ToString());
                }
                else
                {
                    value = dic[item];
                }
                ((IDictionary<string, object>)result).Add(item, value);
                //result.Add(item, value);
            }
            return result;
        }

        public static string Transcoding(string text)
        {
            return Encoding.UTF8.GetString(Encoding.GetEncoding("gb2312").GetBytes(text));
        }
    }
}
